<?php
namespace Admin\Controller;
use Think\Controller;
class LinksController extends AdminController {
    public function index(){
        $lMO = M('links');
        $tCount = $lMO->count();
        $tPage = new \Think\Page($tCount,25);
        $tShow = $tPage->show();
        $lData = $lMO->order('id desc')->limit($tPage->firstRow.','.$tPage->listRows)->select();
        $this->assign('tPage',$tShow);// 赋值分页输出
        $this->assign('lData' , $lData);
        $this->display();
    }

    public function add(){

        $tTime = time();

        if(IS_POST){
           $pName = I('name','','htmlspecialchars');     
           $pUrl = I('url','','validate_url');
           $pSort = I('sort','','int');
           $pDescription = I('description','','htmlspecialchars');
           $pIscheck = I('ischeck','','int');

           if(empty($pName)){
                $this->error('网站名称不能为空');
           }

           $tData = array(
                'name' => $pName,
                'url' => $pUrl,
                'sort' => $pSort,
                'description' => $pDescription,
                'ischeck' => $pIscheck, 
                'created' =>$tTime, 
           );
           $tlMO = M('links');
           if(!$tlMO->add($tData)){
                $this->error('提交失败'); 
           }
           $this->success('新增成功', 'index');
           exit;
        } 
        $this->display();
    }

    public function edit(){

        $tTime = time();
        $pId = I('id' , '', 'int');
        if(empty($pId)){
            $this->error('ID不能为空');
        }

        $tlMO = M('links');
        if(IS_POST){
           $pName = I('name','','htmlspecialchars');     
           $pUrl = I('url','','validate_url');
           $pSort = I('sort','','init');
           $pDescription = I('description','','htmlspecialchars');
           $pIscheck = I('ischeck','','int');

           if(empty($pName)){
                $this->error('网站名称不能为空');
           }

           $tData = array(
                'name' => $pName,
                'url' => $pUrl,
                'sort' => $pSort,
                'description' => $pDescription,
                'ischeck' => $pIscheck, 
                'updated' =>$tTime, 
           );
           if(!$tlMO->where('id=' . $pId)->save($tData)){
                $this->error('提交失败'); 
           }
           $this->success('修改成功', U(MODULE_NAME. '/Links/index'));
           exit;
        } 

        $tRow = $tlMO->where('id=' . $pId)->find(); 
        if(!count($tRow)){
            $this->error('该记录不存在'); 
        }
        $this->assign('tRow' , $tRow);
        $this->display();
    }
    public function del(){
        $pId = I('id' , 0 , 'int'); 
        if(empty($pId)){
            $this->error('ID不能为空'); 
        }

        $lMO = M('links');
        if(!$lMO->where('id = ' .$pId)->delete()){
            $this->error('删除失败'); 
        }
        $this->success('彻底删除成功', U(MODULE_NAME. '/Links/index'));
    }
    public function delall(){
        $tIdarr = I('key',0 , 'intval');  
        if (!is_array($tIdarr)) {
            $this->error('请选择要彻底删除的项');
        }
        $tWhere = array('id' => array('in', $tIdarr));
        $lMO = M('links');
        if(!$lMO->where($tWhere)->delete()){
            $this->error('删除失败'); 
        }

        $this->success('彻底删除成功', U(MODULE_NAME. '/Links/index'));
    }
}
